﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using IFTRUE.DataCollection.BLL;
using IFTRUE.DataCollection.BLL.Custom;

namespace IFTRUE.DataCollection.WCF
{
    /// <summary>
    /// JD 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://iftrue.datacollection.wcf/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class JD : System.Web.Services.WebService
    {
        [WebMethod(Description = "商城列表")]
        public List<Model.mall> MallList()
        {
            return WebPageCollectionBLL.MallList();
        }

        [WebMethod(Description="京东商城商品分类")]
        public List<Model.commodityClass> JDCommodityClass()
        {
            return WebPageCollectionBLL.JDCommodityClass();
        }

        [WebMethod(Description = "京东商城商品分类，初始化到数据库")]
        public string JDCommodityClassToDB()
        {
            BLL.commodityClass commodityClassBLL = new commodityClass();
            if (commodityClassBLL.GetModelList("parentGuid='" + WebPageCollectionBLL.jd_guid + "'").Count>0)
            { 
                return "京东商城商品分类已经存在，需要删除后再能重新初始化";
            }
            List<Model.commodityClass> commodityClassList=WebPageCollectionBLL.JDCommodityClass();

            foreach(Model.commodityClass model in commodityClassList)
            {
                commodityClassBLL.Add(model);
            }
            return "京东商城商品分类初始化完成";
        }

        [WebMethod(Description = "京东商城商品信息")]
        public Model.commodity JSCommodity(string url)
        {
            return WebPageCollectionBLL.JSCommodity(url+"?dt="+DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),System.Text.Encoding.GetEncoding("gb2312"));
        }

        [WebMethod(Description = "京东商城商品信息存储到数据库")]
        public string JSCommodityToDB(long startCode,long endCode)
        {
            Model.commodity model = null;
            BLL.commodity bll = new commodity();
            int count = 0;
            bool bl = false;
            for (long i = startCode; i <= endCode; i++)
            {
                model = WebPageCollectionBLL.JSCommodity(i, System.Text.Encoding.GetEncoding("gb2312"));
                if (model != null)
                {
                    if (bll.ExistsEx(model.code))
                    {
                        bl = bll.UpdateEx(model);
                    }
                    else
                    {
                        model.guid = Guid.NewGuid().ToString();
                        bl = bll.Add(model); }
                    if (bl)
                    {
                        count++;
                        bl = false;
                    }
                }
            }
            return "成功获取京东商品信息：" + count.ToString() + "条";
        }
    }
}
